package lib

import (
	"strconv"
	"strings"

	"github.com/gin-gonic/gin"
)

//array判断
/**
need string
needArr array needArr := []string{"aa","bb","cc","dd"}
**/
func InArray(need interface{}, needArr []interface{}) bool {
	for _, v := range needArr {
		if need == v {
			return true
		}
	}
	return false
}

//get请求所有参数
func Get(c *gin.Context) (map[string]interface{}, map[string]string) {

	where := make(map[string]interface{})
	like := make(map[string]string)
	//	var gets map[string]interface{}
	//	gets["set"] = "vv"
	list := Desc(c.Param("action"))
	arr := []interface{}{"title", "subtitle", "info", "content"}
	for _, v := range list {

		t := strings.Split(v.Type, "(")

		value, err := c.GetQuery(v.Field)

		//fmt.Printf("desc %s=>%s \r\n", v.Field, value)
		if err != false {
			if t[0] == "int" || t[0] == "tinyint" {
				_int, _ := strconv.Atoi(value)
				where[v.Field] = _int
			} else {

				b := InArray(v.Field, arr)
				if b != true {
					where[v.Field] = value
				} else { //后面做好like查询
					like[v.Field] = value
				}

			}

		}
	}
	//fmt.Printf("desc %s=>%s \r\n", where, where)
	/*
		page, err := c.GetQuery("page")
		if err != false {
			where["page"] = page
		}
		pagesize, err := c.GetQuery("pagesize")
		if err != false {
			where["pagesize"] = pagesize
		}

	*/

	return where, like
}

//Post请求所有参数
func Post(c *gin.Context) map[string]string {

	where := make(map[string]string)
	//	like := make(map[string]string)
	//	var gets map[string]interface{}
	//	gets["set"] = "vv"
	list := Desc(c.Param("action"))
	//	arr := []interface{}{"title", "subtitle", "info", "content"}
	for _, v := range list {

		//t := strings.Split(v.Type, "(")
		where[v.Field] = c.PostForm(v.Field)
		/*
			value, err := c.PostForm(v.Field)

			//fmt.Printf("desc %s=>%s \r\n", v.Field, value)
			if err != false {
				if t[0] == "int" || t[0] == "tinyint" {
					_int, _ := strconv.Atoi(value)
					where[v.Field] = _int
				} else {
					where[v.Field] = value

				}

			}
		*/
	}

	return where
}
